// index.js

const app = getApp();
import api from "../../api/api"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    passwordShow: true,
    type: '2',
    username: '',
    passwords: '',
    phonenumber: "",
    token: "",
    navBarHeight: app.globalData.navBarHeight,
    params: {
      code: "",
      phone: "",
      password: "",
      type: ""
    },
  },
  //是否是司机
  isDriver(id){
    try {
      api.post('/companyInfoApp/findDriverStatus', {id:id})
      .then(res => {
        wx.setStorageSync('isdriver', res.data.isDriver)
      })
     } catch (error) {
       console.log("error")
     }
  },
  onChangepassword(event) {
    // event.detail 为当前输入的值
    let that = this
    that.setData({
      passwords: event.detail
    })
  },

  onChangephone(event) {
    // event.detail 为当前输入的值
    let that = this
    that.setData({
      phonenumber: event.detail
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  onShow(){
    wx.clearStorageSync();
  },

  login() {
    let that = this
    if (!/^1[3456789]\d{9}$/.test(that.data.phonenumber)) {
      // 手机号格式不正确，提示用户
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none'
      });
      return false
    } else {
      wx.login({
        success(res) {
          if (res.code) {
            //发起网络请求
            that.setData({
              'params.code': res.code,
              'params.phone': that.data.phonenumber,
              'params.password': that.data.passwords,
              'params.type': that.data.type,
            })
            api.post('/appLogin/userLogin', that.data.params)
              .then(res => {
                // if (!res.data.token) {
                //   wx.showToast({
                //     icon:'none',
                //     title: '未获取到token',
                //     duration: 2000//持续的时间
                //   })
                // }
                if (res.code == 200) {
                  that.isDriver(res.data.id)
                  wx.setStorageSync('token', res.data.token)
                  wx.setStorageSync('type', that.data.type)
                  wx.setStorageSync('id', res.data.id)
                  wx.setStorageSync('isdriver', res.data.is_driver)
                  wx.setStorageSync('barIndex', 0)
                  if (that.data.type == 1) {//公司登录
                    if (res.data.companyId) {
                      wx.setStorageSync('companyid', res.data.companyId)
                      wx.switchTab({
                        url: '/pages/homePage/homePage'
                      })
                      return false
                    } else {
                      wx.navigateTo({
                        url: '/pages/companyList/companyList'
                      })
                      return false
                    }
                  } else {//用户登录
                    wx.setStorageSync('companyid',null)
                    wx.switchTab({
                      url: '/pages/materialFlow/materialFlow'
                    })
                    return false
                  }

                } else if (res.code == 500) {
                  wx.showToast({
                    icon:'none',
                    title: res.message,
                    duration: 2000//持续的时间
                  })
                  return false
                }
              })
            return false
          } else {
            return false
          }
        }
      })
      return false
    }
  },

  dataClick(event) {
    let that = this
    that.setData({
      type: event.currentTarget.dataset.type,
      phonenumber: ""
    })
  },
  onClickIcon(event) {

    this.setData({
      passwordShow: !this.data.passwordShow
    });
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {



  }
})